package org.example.config;

import org.example.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 跨域注册表对象
        registry.addMapping("/**") // 配置后端哪些地址允许跨域
            .allowedOrigins("http://localhost") // 配置允许前端用在什么IP和端口跨域请求
            .allowCredentials(true) // 配置允许前端携带Cookie
            .allowedMethods("*") // 配置允许前端用任意方式请求
            .allowedHeaders("*") // 配置允许前端携带任意请求头部
            .exposedHeaders("*") // 配置允许后端返回任意响应头部
            .maxAge(3600); // 配置预检请求的缓存时间
    }
}
